<?php
/**
 * 材料柜号
 * (model)
 */
namespace App\Models\Product;

use App\Models\Basics\SupplierM;
use Illuminate\Database\Eloquent\Model;

class ProductMaterialContainer extends Model{

    protected $table = 'product_material_container';

    protected $guarded = [];

    /**
     * 柜子类型
     */
    const TYPE_SH = 1; //上海
    const TYPE_SZ  = 2; //深圳
    public static $_type = [
        self::TYPE_SH => 'SH柜(上海)',
        self::TYPE_SZ => 'SZ柜(深圳)'
    ];
    const TYPE_SH_STRING = 'SH';
    const TYPE_SZ_STRING = 'SZ';
    public static $_type_string = [
        self::TYPE_SH => self::TYPE_SH_STRING,
        self::TYPE_SZ => self::TYPE_SZ_STRING
    ];

    /**
     * 状态
     */
    const STATUS_YES = 1; //启用
    const STATUS_NO  = 2; //停用
    public static $_status = [
        self::STATUS_YES => '启用',
        self::STATUS_NO  => '停用'
    ];



    public static function options($where = []){
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        $res = $query->get()->map(function ($model){
            return collect([
                'container' => $model->code.'-'.$model->title,
                'id' => $model->id
            ]);
        });
        return $res->pluck('container','id');
    }

    public static function getModelById($id){
        return self::query()->where('id', $id)->first();
    }

    public static function getModelByCode($code){
        return self::query()->where('code',$code)->first();
    }

    public static function getValueByCode($code,$field){
        return self::query()->where('code',$code)->value($field);
    }

    public static function getValueById($id,$field){
        return self::query()->where('id',$id)->value($field);
    }

}
